﻿package Cain.googleMap.control.zoomControl 
{
	import Cain.control.Bar;
	import Cain.control.BarModel;
	import com.google.maps.Map;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	/**
	 * 自定義縮放控制
	 * @author Cain
	 */
	public class GoogleZoomControl
	{
		private const MAX:Number = 19;
		private const MIN:Number = 4;
		private var zooInBn:DisplayObject;
		private var zooOutBn:DisplayObject;
		private var zooBar:DisplayObject;
		private var zooBarTop:Number;
		private var zooBarFoot:Number;
		private var size:Number;
		private var map:Object;
		private var bar:Bar;
		/**
		 * 自定義縮放控制初始化
		 * @param	map google地圖
		 * @param	_zooIn 放大
		 * @param	_zooOut 縮小
		 * @param	_zooBar 控制
		 * @param	top 最高點y
		 * @param	foot 最低點y
		 */
		public function GoogleZoomControl(_map:Object,_zooIn:DisplayObject = null,_zooOut:DisplayObject = null,_zooBar:DisplayObject = null,top:Number=0,foot:Number=0) 
		{
			map = _map;
			zooInBn = _zooIn;
			zooOutBn = _zooOut;
			zooBar = _zooBar;
			zooBarTop = top;
			zooBarFoot = foot;
			size = map.getZoom();
			init();
		}
		
		private function init():void
		{
			if (zooInBn!=null) 
			{
				zooInBn.addEventListener(MouseEvent.CLICK, zooIn_click);
			}
			if (zooOutBn!=null) 
			{
				zooOutBn.addEventListener(MouseEvent.CLICK, zooOut_click);
			}
			if (zooBar!=null) 
			{
				bar = new Bar(zooBar);
				bar.setup(zooBarTop, zooBarFoot, MAX, MIN, 14);
				bar.addEventListener(BarModel.CHING, bar_ching);//bar數值改變
			}
		}
		//bar數值改變
		private function bar_ching(e:Event):void 
		{
			size = Bar(e.currentTarget).num
			map.setCenter(map.getCenter(), size);
		}
		
		private function zooOut_click(e:MouseEvent):void 
		{
			if (size>MIN+1)
			{
				size = int(size)-1;
				map.setCenter(map.getCenter(), size);
				if (bar!=null) 
				{
					bar.num = size;
				}
			}
		}
		
		private function zooIn_click(e:MouseEvent):void 
		{
			if (size<MAX)
			{
				size = int(size)+1;
				map.setCenter(map.getCenter(), size);
				if (bar!=null) 
				{
					bar.num = size;
				}
			}
		}
		
	}

}